package com.sdcs.ui;

import com.sdcs.R;
import com.sdcs.api.HttpApiAccessor;
import com.sdcs.model.Discuss;

import android.app.ListActivity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

public class AnswerListActivity extends ListActivity {
    private static final int  MENU_DISCUSS  = Menu.FIRST + 4 ;
    private static final int MENU_REFRESH  = Menu.FIRST + 5 ;
    private static final int  MENU_HOME = Menu.FIRST + 6 ; 

	private List<Discuss> list;
	private MyAdapter adapter;

	private int course_id ;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.answer_list);

		course_id =  getIntent().getExtras().getInt("course_id") ;
        
		new LoadTask().execute();
    }
    
    
	public class MyAdapter extends BaseAdapter {

		public int getCount() {
			return list.size();
		}

		public Object getItem(int arg0) {
			return list.get(arg0);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) { 
			convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.answer_item, null);

			Discuss object =  list.get(position) ; 

			TextView tv_wbuser = (TextView) convertView.findViewById(R.id.wbuser);
			tv_wbuser.setText( "评论" + (position + 1)  );  
			
			 
			TextView tv_wbtext = (TextView) convertView.findViewById(R.id.wbtext);
			tv_wbtext.setText( object.getContent() );  
			

			TextView tv_wbtime = (TextView) convertView.findViewById(R.id.wbtime);
			tv_wbtime.setText( object.getTbl_date() );  
			
			TextView tv_answer = (TextView) convertView.findViewById(R.id.answer);
			if( object.getAnswer() == null || object.getAnswer().equals("") ){
				tv_answer.setText( "回答：暂无");
			} else {
				tv_answer.setText( "回答：" + object.getAnswer());
			}

			return convertView;
		}
	}
	
	
	/**
	 * 异步加载所有资源
	 *
	 */
	public class LoadTask extends AsyncTask<Void, Void, Void>{
		
		protected Void doInBackground(Void... arg0) { 
			list =  HttpApiAccessor.getAllDiscussById(course_id) ;
			return null;
		}

		protected void onPostExecute(Void result) {
			adapter = new MyAdapter();
			setListAdapter(adapter);
			removeDialog(0);
			super.onPostExecute(result);
		}

		protected void onPreExecute() {
			showDialog(0);
			super.onPreExecute();
		}
	}
	
		@Override
		public boolean onCreateOptionsMenu(Menu menu) {
			super.onCreateOptionsMenu(menu);

			menu.add(0, MENU_DISCUSS, 0, "评论");
			menu.add(0, MENU_REFRESH, 0, "刷新");
			menu.add(0, MENU_HOME, 0, "返回主页") ;

			return true;
		}
	    

	    @Override
	    public boolean onOptionsItemSelected(MenuItem item) {
	        switch (item.getItemId()) { 
		        case MENU_REFRESH: {
	            	new LoadTask().execute();
	                return true;
	            }
		        case MENU_DISCUSS: {
	        		Intent i2 = new Intent(AnswerListActivity.this , AnswerActivity.class ) ;
	        		i2.putExtra("course_id", course_id) ;
		        	startActivityForResult(i2, 999 );
	                return true;
	            }
	        	case MENU_HOME :
	        		Intent i3 = new Intent(AnswerListActivity.this , MainActivity.class ) ;
		        	startActivity(i3);
		        	finish();
	        		return true;  
	            
	        }
	        return super.onOptionsItemSelected(item);
	    }

		@Override
		protected void onActivityResult(int requestCode, int resultCode, Intent data) {
			// TODO Auto-generated method stub
			super.onActivityResult(requestCode, resultCode, data);

			new LoadTask().execute();
		}
	    
}
